﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;

namespace Rtl.SharePoint
{
    public class USAddressFieldValue : SPFieldMultiColumnValue
    {
        private const int PROP_IDX_ST1 = 0;
        private const int PROP_IDX_ST2 = 1;
        private const int PROP_IDX_CTY = 2;
        private const int PROP_IDX_ST = 3;
        private const int PROP_IDX_ZIP = 4;
        private const int PROP_IDX_LAT = 5;
        private const int PROP_IDX_LON = 6;
        private const int NUM_FIELDS = 7;

        // ctors
        public USAddressFieldValue()
            : base(NUM_FIELDS) { }

        // New field in the SPFieldCollection
        public USAddressFieldValue(string value)
            : base(value) { }

        public string Street1
        {
            get { return this[PROP_IDX_ST1]; }
            set { this[PROP_IDX_ST1] = value; }
        }

        public string Street2
        {
            get { return this[PROP_IDX_ST2]; }
            set { this[PROP_IDX_ST2] = value; }
        }
        public string City
        {
            get { return this[PROP_IDX_CTY]; }
            set { this[PROP_IDX_CTY] = value; }
        }

        public string State
        {
            get { return this[PROP_IDX_ST]; }
            set { this[PROP_IDX_ST] = value; }
        }

        public string Zip
        {
            get { return this[PROP_IDX_ZIP]; }
            set { this[PROP_IDX_ZIP] = value; }
        }

        public double Latitude
        {
            get
            {
                double val;
                if (double.TryParse(this[PROP_IDX_LAT], out val))
                    return val;
                else
                    return double.NaN;
            }
            set
            {
                this[PROP_IDX_LAT] = value.ToString();
            }
        }

        public double Longitude
        {
            get
            {
                double val;
                if (double.TryParse(this[PROP_IDX_LON], out val))
                    return val;
                else
                    return double.NaN;
            }
            set
            {
                this[PROP_IDX_LON] = value.ToString();
            }
        }
    }
}
